package com.ruoyi.project.jiaqin.region.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.region.domain.Region;
import com.ruoyi.project.jiaqin.region.service.IRegionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 行政区域Controller
 * 
 * @author admin
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/jiaqin/region")
public class RegionController extends BaseController
{
    private String prefix = "jiaqin/region";

    @Autowired
    private IRegionService regionService;

    @RequiresPermissions("jiaqin:region:view")
    @GetMapping()
    public String region()
    {
        return prefix + "/region";
    }

    /**
     * 查询行政区域列表
     */
    @RequiresPermissions("jiaqin:region:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Region region)
    {
        startPage();
        List<Region> list = regionService.selectRegionList(region);
        return getDataTable(list);
    }

    /**
     * 导出行政区域列表
     */
    @RequiresPermissions("jiaqin:region:export")
    @Log(title = "行政区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        return util.exportExcel(list, "region");
    }

    /**
     * 新增行政区域
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存行政区域
     */
    @RequiresPermissions("jiaqin:region:add")
    @Log(title = "行政区域", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Region region)
    {
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改行政区域
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Region region = regionService.selectRegionById(id);
        mmap.put("region", region);
        return prefix + "/edit";
    }

    /**
     * 修改保存行政区域
     */
    @RequiresPermissions("jiaqin:region:edit")
    @Log(title = "行政区域", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Region region)
    {
        return toAjax(regionService.updateRegion(region));
    }

    /**
     * 删除行政区域
     */
    @RequiresPermissions("jiaqin:region:remove")
    @Log(title = "行政区域", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(regionService.deleteRegionByIds(ids));
    }
}
